package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebCheckCodeDao;
import ysq.xyg.web.model.po.WebCheckCode;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebCheckCodeVo;
import ysq.xyg.web.service.WebCheckCodeService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-验证码service实现
 *
 * @author shengQiang yu
 * @date 2019-7-25 15:45:39
 */
@Service("webCheckCodeService")
public class WebCheckCodeServiceImpl extends BaseServiceImpl<WebCheckCode> implements WebCheckCodeService {

    @Resource
    private WebCheckCodeDao webCheckCodeDao;

    private WebCheckCode webCheckCode;

    @Autowired
    public void setBaseDao(WebCheckCodeDao webCheckCodeDao) {
        this.baseDao = webCheckCodeDao;
    }


    /**
     * 分页查询
     *
     * @param webCheckCodeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-25 15:45:39
     */
    public ReturnEntity page(WebCheckCodeVo webCheckCodeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebCheckCode> list = acceptList(webCheckCodeVo);
        PageInfo<WebCheckCode> page=new PageInfo<WebCheckCode>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webCheckCodeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-25 15:45:39
     */
    public ReturnEntity addOrUpdate(WebCheckCodeVo webCheckCodeVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webCheckCodeVo.getId())){
            webCheckCode=new WebCheckCode(currentUser);
            isUpate=false;
        }else{
            webCheckCode=selectByPrimaryKey(webCheckCodeVo.getId());
            if(ObjectUtil.isNull(webCheckCode)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webCheckCode.setUpdateTime(new Date());
            webCheckCode.setUpdateUser(currentUser.getId());
        }
        webCheckCode.setUseStatus(webCheckCodeVo.getUseStatus());
        webCheckCode.setPhone(webCheckCodeVo.getPhone());
        webCheckCode.setCodeNo(webCheckCodeVo.getCodeNo());
        webCheckCode.setEndTime(webCheckCodeVo.getEndTime());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webCheckCode);
        }else{
            num=insert(webCheckCode);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-25 15:45:39
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebCheckCode webCheckCode=selectByPrimaryKey(id);
        if(webCheckCode!=null){
            webCheckCode.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webCheckCode.setUpdateTime(new Date());
            webCheckCode.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webCheckCode);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webCheckCodeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-25 15:45:39
     */
    public List<WebCheckCode> acceptList(WebCheckCodeVo webCheckCodeVo){
        Example example = new Example(WebCheckCode.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webCheckCodeVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webCheckCodeVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webCheckCodeVo.getPhone())){
            criteria.andEqualTo("phone",webCheckCodeVo.getPhone());
        }
        if(StringUtil.isNotEmpty(webCheckCodeVo.getCodeNo())){
            criteria.andEqualTo("codeNo",webCheckCodeVo.getCodeNo());
        }
        if(StringUtil.isNotEmpty(webCheckCodeVo.getUseStatus())){
            criteria.andEqualTo("useStatus",webCheckCodeVo.getUseStatus());
        }
        example.setOrderByClause(" update_time desc");
        return webCheckCodeDao.selectByExample(example);
    }
}
